<?php

namespace app\zftpay\controller;

use app\BaseController;
use app\job\ZftSettleConfirmOrder;
use app\service\propertyService;
use app\service\zftService;
use app\zftpay\service\apiService;
use dh2y\qrcode\QRcode;
use think\facade\Db;
use think\facade\Queue;
use think\Image;

class Test extends BaseController
{

    public function bindQuery(){
        $data= array(
            'type'=>"loginName",
            'account'=>"xc85456324@163.com",
            'name'=>"杭州西宸物业服务有限公司",
            'memo'=>"",

            "out_request_no"=>date("YmdHis").rand(1000,9999),
        );
        $api=new zftService();
        $res=$api->royaltyRelationBatchquery(json_encode($data));
        echo "<pre>";
        var_dump($res);
    }
    public function bind(){
        $api=new zftService();
        $data= array(
            'type'=>"loginName",
            'account'=>"xc85456324@163.com",
            'name'=>"杭州西宸物业服务有限公司",
            'memo'=>"",
            'token'=>"202009BB8c8b7a1f312343708117675277305D93",
            "out_request_no"=>date("YmdHis").rand(1000,9999),
        );
        $res=$api->royaltyRelationBind(json_encode($data));
        echo "<pre> ";
        var_dump($res);

    }
    public function bind1(){
        $api=new zftService();
        $data= array(
            'type'=>"loginName",
            'account'=>"linan2421@163.com",
            'name'=>"杭州木耀科技有限公司",
            'memo'=>"",
            'token'=>"202009BB8c8b7a1f312343708117675277305D93",
            "out_request_no"=>date("YmdHis").rand(1000,9999),
        );
        $res=$api->royaltyRelationBind(json_encode($data));
        echo "<pre> ";
        var_dump($res);

    }
    public function settleConfirm(){
        $info=Db::table("zft_order_success")->where("uid","1")->find();
        if(empty($info)){
            var_dump($info);
            return ;
        }
        $propertyZftInfo=(new propertyService())->propertyZftByComId($info["community_id"]);
        if(empty($propertyZftInfo)){
            return json(['info' => '直付通配置为空']);
        }
        $api=new zftService();
        $amount=round($propertyZftInfo["taxrate"]*$info["order_money"],2);
        if($amount<=0){
            Db::table("zft_order_success")->where("id",$info["uid"])->update([
                "settle_status"=>1
            ]);

        }
        $royalty_parameters=array(
            'royalty_type'=>'transfer',//'',
            'trans_in_type'=>'loginName',//收入方账户类型userId
            'trans_in'=> $propertyZftInfo["binding_alipay_logon_id"],//收入方账户\linan2421@163.com  2088070839250462
            'amount'=>$info["order_money"],//分账金额
        );
        $data= array(
            'out_trade_no'=> $info["out_trade_no"],
            'trade_no'=>$info["trade_no"],
            'royalty_parameters'=>$royalty_parameters,
        );
        $result = $api->settleComfirm(json_encode($data));
        $add["result_code"]=$result->code;
        $add["order_uid"]=$info["uid"];
        $add["create_time"]=date("Y-m-d H:i:s");
        if($result->code=="10000"){
            $add["result_msg"]="成功";
            $add["out_request_no"]=$result->out_request_no;
            $add["trade_no"]=$result->trade_no;
            $add["settle_amount"]=$result->settle_amount;
            Db::table("zft_log_settle_confirm")->insert($add);
        }else{
            $add["result_msg"]=$result->sub_msg;
            $add["sub_code"]=$result->sub_code;
            $add["out_request_no"]=$info["out_trade_no"];
            $add["trade_no"]=$info["trade_no"];
            Db::table("zft_log_settle_confirm")->insert($add);
        }
        echo "<pre>";
        var_dump($result);
    }
    public function test4(){
        $jobQueueName  	  = "app\job\ZftSettleQueryOrder";
        $jobData       	  = [ 'id' => "6" ] ;
        $res=Queue::push($jobQueueName,$jobData);
    }
    public function test3(){
        $jobQueueName  	  = "app\job\ZftSettleOrder";
        $jobData       	  = [ 'id' => "3" ] ;
        $res=Queue::push($jobQueueName,$jobData);
    }
    public function test2(){
        $jobQueueName  	  = "app\job\ZftSettleConfirmOrder";
        $jobData       	  = [ 'id' => "7" ] ;
        $res=Queue::push($jobQueueName,$jobData);
        //$res=Queue::push(new ZftSettleConfirmOrder($jobData));
        var_dump($res);
        return ;
    }

    public function order(){
        $info=Db::table("zft_order_success")->where("uid","7")->find();
        var_dump($info);
        if(empty($info)){
            var_dump($info);
            return ;
        }
        $propertyZftInfo=(new propertyService())->propertyZftByComId($info["community_id"]);
        if(empty($propertyZftInfo)){
            return json(['info' => '直付通配置为空']);
        }
        $api=new zftService();
//        $amount=round($propertyZftInfo["taxrate"]*$info["order_money"],2);
//        if($amount<=0){
//            Db::table("zft_order_success")->where("id",$info["uid"])->update([
//                "settle_status"=>1
//            ]);
//            return ;
//
//        }
        $amount=0;
        $royalty_parameters=array(
            'royalty_type'=>'transfer',//'loginName',
            'trans_in_type'=>'loginName',//收入方账户类型
            'trans_in'=>'linan2421@163.com ',//收入方账户\linan2421@163.com
            'amount'=>$amount,//分账金额
        );
        $data= array(
            'out_trade_no'=> date("YmdHis"),//$info["out_trade_no"],//date("YmdHis")
            'trade_no'=>$info["trade_no"],
            'royalty_parameters'=>$royalty_parameters,
        );

        $result = $api->settle(json_encode($data));
        echo "<pre>";
        var_dump($result);
        $add_log["result_code"]=$result->code;
        $add_log["order_uid"]=$info["uid"];
        $add_log["create_time"]=date("Y-m-d H:i:s");
        if($result->code==10000){

            $add_log["out_request_no"]=$data["out_trade_no"];
            $add_log["trade_no"]=$data["trade_no"];
            //$add_log["settle_no"]=$settle["settle_no"];
            Db::table("zft_log_settle_order")->insert($add_log);
        }else{
            $add_log["result_msg"]=$result->sub_msg;
            $add_log["sub_code"]=$result->sub_code;
            $add_log["out_request_no"]=$data["out_trade_no"];
            $add_log["trade_no"]=$data["trade_no"];
            Db::table("zft_log_settle_order")->insert($add_log);
        }

    }
    public function test(){


        $info=Db::table("zft_order_success")->where("uid","1")->find();
        var_dump($info);
        if(empty($info)){
            var_dump($info);
            return ;
        }
        $propertyZftInfo=(new propertyService())->propertyZftByComId($info["community_id"]);
        if(empty($propertyZftInfo)){
            return json(['info' => '直付通配置为空']);
        }
        $api=new zftService();
        $amount=round($propertyZftInfo["taxrate"]*$info["order_money"],2);
        if($amount<=0){
            Db::table("zft_order_success")->where("id",$info["uid"])->update([
                "settle_status"=>1
            ]);
            return ;

        }
        $royalty_parameters=array(
            'royalty_type'=>'transfer',//'loginName',
            'trans_in_type'=>'loginName',//收入方账户类型
            'trans_in'=>'aliwykd@163.com',//linan2421@163.com ',//收入方账户\linan2421@163.com
            'amount'=>$amount,//分账金额
        );
        $data= array(
            'out_trade_no'=> $info["out_trade_no"],//$info["out_trade_no"],//date("YmdHis")
            'trade_no'=>$info["trade_no"],
            'royalty_parameters'=>$royalty_parameters,
        );

        $result = $api->settle(json_encode($data));
        echo "<pre>";
        var_dump($result);
        $add_log["result_code"]=$result->code;
        $add_log["order_uid"]=$info["uid"];
        $add_log["create_time"]=date("Y-m-d H:i:s");
        if($result->code==10000){
            $settle["community_id"]=$info["community_id"];
            $settle["trade_no"]=$info["trade_no"];
            $settle["order_uid"]=$info["uid"];
            $settle["order_money"]=$info["pay_money"];
            $settle["settle_money"]=$amount;
            $settle["community_name"]=$info["community_name"];
            $settle["settle_no"]=$result->settle_no;
            $settle["createTime"]=time();
            $res=Db::table("zft_settle_order")->insert($settle);
            $add_log["out_request_no"]=$data["out_trade_no"];
            $add_log["trade_no"]=$data["trade_no"];
            $add_log["settle_no"]=$settle["settle_no"];
            Db::table("zft_log_settle_order")->insert($add_log);
        }else{
            $add_log["result_msg"]=$result->sub_msg;
            $add_log["sub_code"]=$result->sub_code;
            $add_log["out_request_no"]=$data["out_trade_no"];
            $add_log["trade_no"]=$data["trade_no"];
            $logId=Db::table("zft_log_settle_order")->insertGetId($add_log);
            $settle["community_id"]=$info["community_id"];
            $settle["trade_no"]=$info["trade_no"];
            $settle["order_uid"]=$info["uid"];
            $settle["order_money"]=$info["pay_money"];
            $settle["settle_money"]=$amount;
            $settle["community_name"]=$info["community_name"];
            $settle["createTime"]=time();
            $settle["ctime"]=date("Y-m-d H:i:s");
            $settle["fail_msg"]=$add_log["result_msg"];
            $settle["log_settle_order_id"]=$logId;
            $res=Db::table("zft_settle_order_fail")->insert($settle);
        }

    }

    public function settleQuery(){
        $info=Db::table("zft_settle_order")->where("id","4")->find();
        if(!empty($info)){
            $query["settle_no"]=$info["settle_no"];//$info["settle_no"];
            $api=new zftService();
            $result = $api->settleQuery(json_encode($query));
            echo "<pre>";
            var_dump($result);

            $add_log["result_code"]=$result->code;
            $add_log["settle_order_id"]=$info["id"];
            $add_log["settle_no"]=$info["settle_no"];
            $add_log["create_time"]=date("Y-m-d H:i:s");
            if($result->code==10000){
                $add_log["result_msg"]=$result->msg;
                $add_log["out_request_no"]=$result->out_request_no;
                $add_log["operation_dt"]=$result->operation_dt;
                $add_log["royalty_detail_list"]=json_encode($result->royalty_detail_list);
                Db::table("zft_settle_order")->where("id",$info["id"])->update(["status"=>1]);
                Db::table("zft_order_success")->where("uid",$info["order_uid"])->update(["settle_status"=>1]);
                Db::table("zft_log_settle_order_query")->insert($add_log);
            }else{
                $add_log["result_msg"]=$result->msg;
                $add_log["sub_code"]=$result->sub_code;

                Db::table("zft_log_settle_order_query")->insert($add_log);
            }
        }
    }
    public function settleQuery2(){
        $query["trade_no"]="2025040222001445471415496942";//$info["settle_no"];
        $query["out_request_no"]="2025040221175151715391";
        $api=new zftService();
        $result = $api->settleQuery2(json_encode($query));
        echo "<pre>";
        var_dump($result);
        //$info=Db::table("zft_settle_order")->where("id","1")->find();

    }
}